from django.http import HttpResponse
from market.chat import chatSettings
from market.chat.boshclient import BOSHClient
from market.authentification import authentificateUser
from market.models import UserProfile, Player, BetRoom
from django.utils import simplejson
from market.exceptionHandler import handleRequestBetroomPOSTWithException


def manageChatConnection0(request, betroom ):
        bc = BOSHClient(
                request.user.username.lower(),
                betroom.name
                )
        try:
                bc.startSessionAndAuth()
                return HttpResponse(
                        simplejson.dumps( [{
                                'jid': bc.jabberid.full(),
                                'sid': bc.sid, 'rid': bc.rid,
                                'BOSH_SERVICE':chatSettings.BOSH_SERVICE,
                                'chatRoomName':betroom.name,
                                'chatRoomExtension':chatSettings.CHATROOM_ADDRESS_EXTENSION,
                                'playerName':request.user.username,
                                'betroomName':betroom.name,
                                'speakingAllowed':betroom.allowPlayerToSpeak
                                }] ),
                        mimetype='application/javascript'
                        )

        except Exception as e:
                return HttpResponse( simplejson.dumps( ['error: ' + str( e )] ), mimetype='application/javascript' )

def manageChatConnection(request):
        return handleRequestBetroomPOSTWithException(  globals()["manageChatConnection0"], request )

